package com.nb.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nb.dao.CategoryDao;
import com.nb.domain.Category;
import com.nb.domain.Dish;
import com.nb.domain.Setmeal;
import com.nb.result.CustomException;
import com.nb.service.CategoryService;
import com.nb.service.DishService;
import com.nb.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
   private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        //根据id查询该分类
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId,id);

        LambdaQueryWrapper<Setmeal> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(Setmeal::getCategoryId ,id );

        //判断该分类下是否有菜品、套餐
        int count = dishService.count(lqw);

        int count1 = setmealService.count(lqw1);

        //如果有菜品，抛出有异常
        if (count > 0) {
            throw new  CustomException("该分类关联菜品，请删除菜品后再删除！");
        }
        //如果有套餐，抛异常
        if (count1 > 0){
            throw new CustomException("该分类关联套餐，请删除菜品后再删除！");
        }

        //正常删除
        super.removeById(id);

    }
}
